package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"three/api/request"
	"three/internal/dao"
	"three/internal/model"
	"three/internal/queue"
)

func AddLive(c *gin.Context) {
	var req request.AddLive
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	live := model.Live{
		Name: req.Name,
		Sex:  req.Sex,
		Gift: req.Gift,
	}
	if !dao.Create(&live) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "添加失败",
			"data": nil,
		})
		return
	}
	queue.QueuePub("huangziyu", req)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": live,
	})
	return
}

func FindLive(c *gin.Context) {
	var req request.FindLive
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	var live []model.Live
	if !dao.Find(&live) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "查询失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": live,
	})
	return
}

func UpdateLive(c *gin.Context) {
	var req request.UpdateLive
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	live := model.Live{
		Id:     req.Id,
		Status: req.Status,
	}
	if !dao.Update(&live) {
		c.JSON(http.StatusOK, gin.H{
			"code": 500,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": live,
	})
	return
}
